import auth from '@/api/auth'
import { getToken, setToken, removeToken } from '@/utils/auth'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    permissions: []
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_PERMISSIONS: (state, permissions) => {
    state.permissions = permissions
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      auth.login({ username: username.trim(), password: password }).then(response => {
        
        const {data} = response
        commit('SET_TOKEN', data.token)
        
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
     auth.getUserInfo().then(response => {
        const { data } = response
        if (!data) {
          return reject('Verification failed, please Login again.')
        }
        // 适配后端返回的数据结构
        const { username, avatar, permissions = [] } = data
        
        // 如果没有权限数据，设置默认权限以便测试
        const finalPermissions = permissions.length > 0 ? permissions : [
          'device:list:add', 'device:list:edit', 'device:list:delete', 'device:list:detail',
          'maintenance:apply:add', 'maintenance:apply:edit', 'maintenance:apply:delete', 'maintenance:apply:detail',
          'inspection:list:add', 'inspection:list:edit', 'inspection:list:delete', 'inspection:list:detail',
          'system:menu:add', 'system:menu:edit', 'system:menu:delete',
          'system:user:add', 'system:user:edit', 'system:user:delete', 'system:user:assignRole', 'system:user:resetPwd',
          'system:role:add', 'system:role:edit', 'system:role:delete', 'system:role:perms'
        ]

        commit('SET_NAME', username)
        commit('SET_AVATAR', avatar)
        commit('SET_PERMISSIONS', finalPermissions)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      auth.logout().then(() => {
        removeToken() // must remove  token  first
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

const getters = {
  permissions: (state) => state.permissions
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}

